﻿Imports System.IO
Imports System.Web
Imports System.Text
Imports System.Xml
Imports System.Xml.Linq
Imports MeoTom.Core.DAL

Namespace BLL
    Public Class Articles
        Public Util As New Utilities.Common()

        Private _isTopPage As Boolean
        Private _userId As Integer
        Private _articlePath As String
        Private _articlesId As Integer
        Private _title As String
        Private _abstract As String
        Private _approved As Boolean
        Private _commentEnabled As Boolean
        Private _viewCount As Integer
        Private _votes As Double
        Private _totalRating As Integer
        Private _categoryId As Integer
        Private _childCatId As Integer
        Private _createdDate As DateTime
        Private _expireDate As DateTime
        Private _releaseDate As DateTime
        Private _imagePath As String
        Private _fromDate As DateTime
        Private _toDate As DateTime
        Private _catName As String
        Private _parentCat As Integer
        Private _parentName As String
        Private _isMedia As Boolean
        Private _isPicture As Boolean
        Private _titleNone As String
        Private _articlePassword As String
        Private _body As String
        Private _postVisible As Integer
        Private _tagId As Integer
        Private _vByDate As DateTime
        Private _catNameNone As String
        Private _isVote As Boolean
        Private _arrayCatId As String
        Private _price As String

        Public Property Price() As String
            Get
                Return _Price
            End Get
            Set(ByVal value As String)
                _Price = value
            End Set
        End Property
        Public Property ArrayCatId() As String
            Get
                Return _arrayCatId
            End Get
            Set(ByVal value As String)
                _arrayCatId = value
            End Set
        End Property
        Public Property ParentName() As String
            Get
                Return _ParentName
            End Get
            Set(ByVal value As String)
                _ParentName = value
            End Set
        End Property
        Public Property ChildCatId() As Integer
            Get
                Return _ChildCatId
            End Get
            Set(ByVal value As Integer)
                _ChildCatId = value
            End Set
        End Property
        Public Property IsVote() As Boolean
            Get
                Return _IsVote
            End Get
            Set(ByVal value As Boolean)
                _IsVote = value
            End Set
        End Property
        Public Property IsTopPage() As Boolean
            Get
                Return _IsTopPage
            End Get
            Set(ByVal value As Boolean)
                _IsTopPage = value
            End Set
        End Property
        Public Property CatNameNone() As String
            Get
                Return _CatNameNone
            End Get
            Set(ByVal value As String)
                _CatNameNone = value
            End Set
        End Property
        Public Property VByDate() As DateTime
            Get
                Return _vByDate
            End Get
            Set(ByVal value As DateTime)
                _vByDate = value
            End Set
        End Property
        Public Property TagId() As Integer
            Get
                Return _tagID
            End Get
            Set(ByVal value As Integer)
                _tagID = value
            End Set
        End Property
        Public Property UserId() As Integer
            Get
                Return _userID
            End Get
            Set(ByVal value As Integer)
                _userID = value
            End Set
        End Property
        Public Property ArticlePath() As String
            Get
                Return _ArticlePath
            End Get
            Set(ByVal value As String)
                _ArticlePath = value
            End Set
        End Property
        Public Property PostVisible() As String
            Get
                Return _PostVisible
            End Get
            Set(ByVal value As String)
                _PostVisible = value
            End Set
        End Property
        Public Property Body() As String
            Get
                Return _Body
            End Get
            Set(ByVal value As String)
                _Body = value
            End Set
        End Property
        Public Property ArticlePassword() As String
            Get
                Return _ArticlePassword
            End Get
            Set(ByVal value As String)
                _ArticlePassword = value
            End Set
        End Property
        Public Property TitleNone() As String
            Get
                Return _TitleNone
            End Get
            Set(ByVal value As String)
                _TitleNone = value
            End Set
        End Property
        Public Property ParentCat() As Integer
            Get
                Return _ParentCat
            End Get
            Set(ByVal value As Integer)
                _ParentCat = value
            End Set
        End Property
        Public Property IsMedia() As Boolean
            Get
                Return _IsMedia
            End Get
            Set(ByVal value As Boolean)
                _IsMedia = value
            End Set
        End Property
        Public Property IsPicture() As Boolean
            Get
                Return _IsPicture
            End Get
            Set(ByVal value As Boolean)
                _IsPicture = value
            End Set
        End Property
        Public Property TotalComment() As Integer
            Get
                Return _TotalRating
            End Get
            Set(ByVal value As Integer)
                _TotalRating = value
            End Set
        End Property
        Public Property CatName() As String
            Get
                Return _CatName
            End Get
            Set(ByVal value As String)
                _CatName = value
            End Set
        End Property
        Public Property FromDate() As DateTime
            Get
                Return _FromDate
            End Get
            Set(ByVal value As DateTime)
                _FromDate = value
            End Set
        End Property
        Public Property ToDate() As DateTime
            Get
                Return _ToDate
            End Get
            Set(ByVal value As DateTime)
                _ToDate = value
            End Set
        End Property
        Public Property ArticlesId() As Integer
            Get
                Return _ArticlesID
            End Get
            Set(ByVal value As Integer)
                _ArticlesID = value
            End Set
        End Property
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property
        Public Property Abstract() As String
            Get
                Return _Abstract
            End Get
            Set(ByVal value As String)
                _Abstract = value
            End Set
        End Property
        Public Property Approved() As Boolean
            Get
                Return _Approved
            End Get
            Set(ByVal value As Boolean)
                _Approved = value
            End Set
        End Property
        Public Property CommentEnabled() As Boolean
            Get
                Return _CommentEnabled
            End Get
            Set(ByVal value As Boolean)
                _CommentEnabled = value
            End Set
        End Property
        Public Property ViewCount() As Integer
            Get
                Return _ViewCount
            End Get
            Set(ByVal value As Integer)
                _ViewCount = value
            End Set
        End Property
        Public Property Votes() As Double
            Get
                Return _Votes
            End Get
            Set(ByVal value As Double)
                _Votes = value
            End Set
        End Property
        Public Property TotalRating() As Integer
            Get
                Return _TotalRating
            End Get
            Set(ByVal value As Integer)
                _TotalRating = value
            End Set
        End Property
        Public Property CategoryId() As Integer
            Get
                Return _CategoryId
            End Get
            Set(ByVal value As Integer)
                _CategoryId = value
            End Set
        End Property
        Public Property CreatedDate() As DateTime
            Get
                Return _CreatedDate
            End Get
            Set(ByVal value As DateTime)
                _CreatedDate = value
            End Set
        End Property
        Public Property ExpireDate() As DateTime
            Get
                Return _ExpireDate
            End Get
            Set(ByVal value As DateTime)
                _ExpireDate = value
            End Set
        End Property
        Public Property ReleaseDate() As DateTime
            Get
                Return _ReleaseDate
            End Get
            Set(ByVal value As DateTime)
                _ReleaseDate = value
            End Set
        End Property
        Public Property ImagePath() As String
            Get
                Return _ImagePath
            End Get
            Set(ByVal value As String)
                _ImagePath = value
            End Set
        End Property

        Public ArticlePrv As ArticleProvider
        Public T As New Tags()

        Public Sub New()
            articlePrv = New ArticleProvider()
        End Sub

        Public Sub Create()
            Dim reader As SqlClient.SqlDataReader = articlePrv.Create(Me)
            If reader.Read() Then
                ArticlesId = Integer.Parse(reader("ArticlesID").ToString())
                IsMedia = Boolean.Parse(reader("IsMedia").ToString())
                IsPicture = Boolean.Parse(reader("IsPicture").ToString())
                Title = reader("Title").ToString()
                Abstract = HttpContext.Current.Server.HtmlDecode(reader("Abstract").ToString())
                CommentEnabled = Boolean.Parse(reader("CommentsEnabled").ToString())
                ViewCount = Integer.Parse(reader("ViewCount").ToString())
                ReleaseDate = DateTime.Parse(reader("ReleaseDate").ToString())
                CreatedDate = DateTime.Parse(reader("CreatedDate").ToString())
                TotalComment = Integer.Parse(reader("TotalComment").ToString())
                ArticlePassword = reader("ArticlePassword").ToString()
                PostVisible = reader("PostVisible").ToString()
                ArticlePath = reader("ArticlePath").ToString()
                Approved = Boolean.Parse(reader("Approved").ToString())
                ImagePath = reader("ImagePath").ToString()
            End If
            reader.Close()
        End Sub

        Public Sub CreateProduct()
            Dim reader As SqlClient.SqlDataReader = ArticlePrv.CreateProduct(Me)
            If reader.Read() Then
                ArticlesId = Integer.Parse(reader("ArticlesID").ToString())
                Title = reader("Title").ToString()
                ViewCount = Integer.Parse(reader("ViewCount").ToString())
                ReleaseDate = DateTime.Parse(reader("ReleaseDate").ToString())
                CreatedDate = DateTime.Parse(reader("CreatedDate").ToString())
                PostVisible = reader("PostVisible").ToString()
                ArticlePath = reader("ArticlePath").ToString()
                ImagePath = reader("ImagePath").ToString()
            End If
            reader.Close()
        End Sub

        Public Sub Update()
            ArticlePrv.Update(Me)
        End Sub

        Public Sub Delete()
            ArticlePrv.Delete(Me)
        End Sub

        Public Sub SetTempArticleCat(ByVal t As Integer)
            ArticlePrv.SetTempArticleCat(Me, t)
        End Sub

        Public Sub RemoveTempArticleCat()
            ArticlePrv.RemoveTempArticleCat(Me)
        End Sub

        Public Sub UpdateArticlePath()
            ArticlePrv.UpdateArticlePath(Me)
        End Sub

        Public Function GetAll() As DataSet
            Return ArticlePrv.GetAll(Me)
        End Function

        Public Function GetByCatInside() As SqlClient.SqlDataReader
            Return ArticlePrv.GetByCatInside(Me)
        End Function

        Public Function GetByCatInside2() As DataSet
            Return ArticlePrv.GetByCatInside2(Me)
        End Function

        Public Function GetByCat() As DataSet
            Return ArticlePrv.GetByCat(Me)
        End Function

        Public Function GetByTagInside() As DataSet
            Return ArticlePrv.GetByTagInside(Me)
        End Function

        Public Function GetArticle() As DataSet
            Return ArticlePrv.GetArticle()
        End Function

        Public Function Find(ByVal strKeyWord As String) As DataSet
            Return ArticlePrv.Find(strKeyWord)
        End Function

        Public Sub GetProEdit()
            Dim reader As SqlClient.SqlDataReader = ArticlePrv.GetByEdit(Me)
            If reader.Read() Then
                ArticlesId = Integer.Parse(reader("ArticlesID").ToString())
                CreatedDate = DateTime.Parse(reader("CreatedDate").ToString())
                ImagePath = reader("ImagePath").ToString()
                ReleaseDate = DateTime.Parse(reader("ReleaseDate").ToString())
                Title = reader("Title").ToString()
                TitleNone = reader("TitleNone").ToString()
                ArticlePath = reader("ArticlePath").ToString()
                Price = reader("Price").ToString

                Dim sPath As String = Configs.ArticleFile & "ListFile\" & CreatedDate.Year.ToString & "\" & CreatedDate.Month.ToString & "\" & CreatedDate.Day.ToString() & "\" & ArticlesId.ToString & ".xml"
                If File.Exists(sPath) Then
                    Dim xmldoc As New XmlDocument()
                    xmldoc.Load(sPath)

                    Dim nodelist As XmlNodeList = xmldoc.SelectNodes("Articles/Article")
                    Dim nodedetail As XmlNode

                    nodedetail = nodelist.Item(0).SelectSingleNode("Body")
                    Body = nodedetail.InnerText
                End If
            End If
            reader.Close()
        End Sub

        Public Sub GetByEdit()
            Dim reader As SqlClient.SqlDataReader = ArticlePrv.GetByEdit(Me)
            If reader.Read() Then
                IsMedia = Boolean.Parse(reader("IsMedia").ToString())
                IsPicture = Boolean.Parse(reader("IsPicture").ToString())
                Approved = Boolean.Parse(reader("Approved").ToString())
                ArticlesId = Integer.Parse(reader("ArticlesID").ToString())
                Abstract = HttpContext.Current.Server.HtmlDecode(reader("Abstract").ToString())
                CommentEnabled = Boolean.Parse(reader("CommentsEnabled").ToString())
                CreatedDate = DateTime.Parse(reader("CreatedDate").ToString())
                ExpireDate = DateTime.Parse(reader("ExpireDate").ToString())
                ImagePath = reader("ImagePath").ToString()
                ReleaseDate = DateTime.Parse(reader("ReleaseDate").ToString())
                Title = reader("Title").ToString()
                TitleNone = reader("TitleNone").ToString()
                PostVisible = reader("PostVisible").ToString()
                ArticlePassword = reader("ArticlePassword").ToString()
                ArticlePath = reader("ArticlePath").ToString()
                IsTopPage = Boolean.Parse(reader("IsTopPage").ToString())
                IsVote = Boolean.Parse(reader("IsVote").ToString())

                Dim sPath As String = Configs.ArticleFile & "ListFile\" & CreatedDate.Year.ToString & "\" & CreatedDate.Month.ToString & "\" & CreatedDate.Day.ToString() & "\" & ArticlesId.ToString & ".xml"
                If File.Exists(sPath) Then
                    Dim xmldoc As New XmlDocument()
                    xmldoc.Load(sPath)

                    Dim nodelist As XmlNodeList = xmldoc.SelectNodes("Articles/Article")
                    Dim nodedetail As XmlNode

                    nodedetail = nodelist.Item(0).SelectSingleNode("Body")
                    Body = nodedetail.InnerText
                End If
            End If
            reader.Close()
        End Sub

        Public Sub GetById()
            Dim reader As SqlClient.SqlDataReader = ArticlePrv.GetByID(Me)
            If reader.Read() Then
                ArticlesId = Integer.Parse(reader("ArticlesID").ToString())
                IsMedia = Boolean.Parse(reader("IsMedia").ToString())
                IsPicture = Boolean.Parse(reader("IsPicture").ToString())
                Title = reader("Title").ToString()
                ArticlePath = reader("ArticlePath").ToString()
                Abstract = HttpContext.Current.Server.HtmlDecode(reader("Abstract").ToString())
                CommentEnabled = Boolean.Parse(reader("CommentsEnabled").ToString())
                ViewCount = Integer.Parse(reader("ViewCount").ToString())
                ReleaseDate = DateTime.Parse(reader("ReleaseDate").ToString())
                TotalComment = Integer.Parse(reader("TotalComment").ToString())
                ImagePath = reader("ImagePath").ToString()
                ArticlePassword = reader("ArticlePassword").ToString
                PostVisible = reader("PostVisible").ToString()
            End If
            reader.Close()
        End Sub

        Public Function GetOlder() As DataSet
            Return ArticlePrv.GetOlder(Me)
        End Function

        Public Function GetAllCat(ByVal t As Integer) As String
            Dim strOut As New StringBuilder()
            Dim reader As SqlClient.SqlDataReader = ArticlePrv.GetAllCat(Me)
            If reader.HasRows Then
                While reader.Read
                    If t = 0 Then
                        'get from inside
                        strOut.Append("<a href=""ArticleUpdate.aspx?action=getByCat&CatID=").Append(reader("CatID").ToString()).Append(""">").Append(reader("Title").ToString()).Append("</a>,")
                    ElseIf t = 2 Then
                        strOut.Append("<a href=""/c").Append(reader("CatID").ToString()).Append("s").Append(reader("SubCat").ToString).Append("/").Append(reader("TitleNone").ToString()).Append(""" title=""").Append(reader("Title").ToString()).Append(""">").Append(reader("Title").ToString()).Append("</a>,")
                    ElseIf t = 1 Then
                        'get CatID to edit
                        strOut.Append(reader("CatID").ToString()).Append(",")
                    End If
                End While
            End If
            reader.Close()
            Return strOut.ToString()
        End Function

        Public Function GetAllTag(ByVal t As Integer) As String
            Dim strOut As New StringBuilder()
            Dim reader As SqlClient.SqlDataReader = ArticlePrv.GetAllTag(Me)
            If reader.HasRows Then
                While reader.Read
                    If t = 0 Then
                        'get from inside
                        strOut.Append("<a href=""ArticleUpdate.aspx?action=getByTag&TagID=").Append(reader("TagID").ToString()).Append(""">").Append(reader("TagName").ToString()).Append("</a>,")
                    ElseIf t = 1 Then

                    End If
                End While
            End If
            reader.Close()
            Return strOut.ToString()
        End Function

        Public Sub UpdateViewCount()
            ArticlePrv.UpdateViewCount(Me)
        End Sub

        Public Sub UpdateViewCountMonth()
            ArticlePrv.UpdateViewCountMonth()
        End Sub

        Public Sub WriteTop10Last()
            Dim strOut As New StringBuilder()
            Try
                strOut.Append("<div class=""otherBox fl""><h3>Bài mới nhất:</h3><div class=""hr fl"">&nbsp;</div><div class=""content-box""><ul>")
                Dim reader As SqlClient.SqlDataReader = ArticlePrv.GetTop10Last()
                While reader.Read()
                    strOut.Append("<li><a href=""" & reader("ArticlePath").ToString() & """ title=""" & reader("Title").ToString() & """>" & reader("Title").ToString() & "</a></li>")
                End While
                reader.Close()
                strOut.Append("</ul></div></div>")

                Using writer As New StreamWriter(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Top10Last.htm", False, Encoding.UTF8)
                    writer.Write(strOut.ToString())
                    writer.Flush()
                    writer.Close()
                End Using
            Catch ex As Exception
            Finally
            End Try
        End Sub

        Public Sub WriteTop10View()
            Dim strOut As New StringBuilder()
            Try
                strOut.Append("<div class=""otherBox fl""><h3>Bài xem nhiều:</h3><div class=""hr fl"">&nbsp;</div><div class=""content-box""><ul>")

                Dim reader As SqlClient.SqlDataReader = ArticlePrv.GetTop10View()
                While reader.Read()
                    strOut.Append("<li><a href=""" & reader("ArticlePath").ToString() & """ title=""" & reader("Title").ToString() & """>" & reader("Title").ToString() & "</a></li>")
                End While
                reader.Close()

                strOut.Append("</ul></div></div>")

                Using writer As New StreamWriter(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Top10View.htm", False, Encoding.UTF8)
                    writer.Write(strOut.ToString())
                    writer.Flush()
                    writer.Close()
                End Using
            Catch ex As Exception
            Finally
            End Try
        End Sub

        Public Sub WriteArchive()
            Dim isFirst As Boolean = True
            Dim strMonth As String
            Dim strYear As String
            Dim strLastYear As String = ""
            Dim strOut As New StringBuilder

            strOut.Append("<div class=""otherBox fl""><h3>Lưu trữ</h3><div class=""hr fl"">&nbsp;</div><div class=""content-box archive""><ul>")
            Dim reader As SqlClient.SqlDataReader = ArticlePrv.GetArchive()
            While reader.Read()
                strMonth = Left(Right("00" & reader("Name").ToString, 7), 2)
                strYear = Right(Right("00" & reader("Name").ToString, 7), 4)

                If isFirst Then
                    strOut.Append("<li class=""isYear"">Năm ").Append(strYear).Append("</li>")
                ElseIf Integer.Parse(strYear) <> Integer.Parse(strLastYear) Then
                    strOut.Append("<li class=""isYear"">Năm ").Append(strYear).Append("</li>")
                End If

                strOut.Append("<li>")
                strOut.Append("     <a href=""/").Append(strYear).Append("/").Append(strMonth).Append(""" title=""Xem bài viết trong thán ").Append(strMonth).Append(" năm ").Append(strYear).Append(""">Tháng " & strMonth & " (").Append(reader("value").ToString).Append(" bài)</a>")
                strOut.Append("</li>")

                strLastYear = strYear
                isFirst = False
            End While
            reader.Close()
            strOut.Append("</ul></div></div>")

            Using writer As New StreamWriter(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Archive.htm", False, Encoding.UTF8)
                writer.Write(strOut.ToString())
                writer.Flush()
                writer.Close()
            End Using
        End Sub

        Public Function ReadTop10View() As String
            Dim strOut As String
            Using reader As New StreamReader(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Top10View.htm", Encoding.UTF8)
                strOut = reader.ReadToEnd()
            End Using
            Return strOut
        End Function

        Public Function ReadTop10Last() As String
            Dim strOut As String
            Using reader As New StreamReader(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Top10Last.htm", Encoding.UTF8)
                strOut = reader.ReadToEnd()
            End Using
            Return strOut
        End Function

        Public Function GetTopStory() As String
            Dim strWrite As New StringBuilder()
            Try
                Dim reader As SqlClient.SqlDataReader = ArticlePrv.GetTopStory()

                strWrite.Append("<div class=""content-post fl""><h2>Bài mới nhất:</h2><div class=""hr fl"">&nbsp;</div>")
                While reader.Read
                    Const strVideo As String = "&nbsp;<img src=""/images/hasVideo.gif"" style=""padding:0 0px 5px 0;float:none;"" width=""15"" height=""9"" alt=""Video"" />"
                    Const strImage As String = "&nbsp;<img src=""/images/HasImage.gif"" style=""padding:0 0px 5px 0;float:none;"" width=""10"" height=""10"" alt=""Images"" />"
                    Dim strMedia As String = ""

                    T.ArticleID = Integer.Parse(reader("ArticlesID").ToString())
                    ArticlesId = T.ArticleID

                    If Boolean.Parse(reader("IsMedia").ToString()) Then
                        strMedia &= strVideo
                    End If

                    If Boolean.Parse(reader("IsPicture").ToString()) Then
                        strMedia &= strImage
                    End If

                    strWrite.Append("<div class=""post-detail fl"">")
                    strWrite.Append("<div class=""post-detail-left fl"">")
                    strWrite.Append("<div class=""post-detail-bg""><span class=""total-cm"">")
                    strWrite.Append(reader("TotalComment").ToString)
                    strWrite.Append("</span><br />Comments<br />")
                    strWrite.Append(reader("ViewCount").ToString())
                    strWrite.Append(" Views</div>")
                    strWrite.Append("</div>")
                    strWrite.Append("<div class=""post-detail-right fl""><h3>")
                    strWrite.Append("<a href=""").Append(reader("ArticlePath").ToString).Append(""" title=""").Append(reader("Title").ToString).Append(""">").Append(reader("Title").ToString).Append("</a>").Append(strMedia)
                    strWrite.Append("</h3>")
                    strWrite.Append("<div class=""post-desc"">")
                    strWrite.Append(HttpContext.Current.Server.HtmlDecode(reader("Abstract").ToString)).Append("...")
                    strWrite.Append("</div>")
                    strWrite.Append("<div class=""post-info"">")
                    strWrite.Append("<a href=""").Append(reader("ArticlePath").ToString).Append(""" title=""").Append(reader("Title").ToString).Append(""">").Append(Format(DateTime.Parse(reader("ReleaseDate").ToString).AddHours(Configs.TimeLine), "dd-MM-yyyy HH:mm tt"))
                    strWrite.Append("</a> bởi <a href=""/Users/").Append(reader("UserName").ToString()).Append(""" title=""").Append(reader("UserName").ToString()).Append(""">").Append(reader("UserName").ToString()).Append("</a>")
                    strWrite.Append(" mục ").Append(GetAllCat(2))
                    If T.GetTag().Length > 1 Then
                        strWrite.Append("<br />Tags: ").Append(T.GetTag())
                    End If
                    strWrite.Append("</div>")
                    strWrite.Append("</div>")
                    strWrite.Append("</div><div class=""hr cut94 fl"">&nbsp;</div>")
                End While
                reader.Close()
                strWrite.Append("<div class=""post-footer2""><a href=""/blogs?Page=2").Append(""" title=""Xem tất cả bài viết ..."">Xem tất cả bài viết...</a></div>")
                strWrite.Append("</div>")
            Catch ex As Exception
            Finally

            End Try
            Return strWrite.ToString
        End Function

        Public Sub WriteTopStory()
            Using writer As New StreamWriter(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_TopStory.htm", False, Encoding.UTF8)
                writer.Write(GetTopStory)
            End Using
        End Sub

        Public Function ReadTopStory() As String
            Dim strReturn As String
            Using reader As New StreamReader(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_TopStory.htm", Encoding.UTF8)
                strReturn = reader.ReadToEnd()
            End Using
            Return strReturn
        End Function

        Public Sub ReadXmlbyId(ByVal id As String, ByVal day As String, ByVal month As String, ByVal year As String)
            Dim sPath As String = Configs.ArticleFile & "ListFile\" & year & "\" & month & "\" & day & "\" & id & ".xml"
            If File.Exists(sPath) Then
                Dim articleXml As XDocument = XDocument.Load(sPath)
                Dim articleSource = (From x In articleXml.Descendants("Article") _
                                  Select ArticlesID = x.Element("ArticlesID").Value, _
                                  Title = x.Element("Title").Value, _
                                  Body = x.Element("Body").Value, _
                                  CommentEnabled = Boolean.Parse(x.Element("CommentEnabled").Value), _
                                  ViewCount = Integer.Parse(x.Element("ViewCount").Value), _
                                  ReleaseDate = DateTime.Parse(x.Element("ReleaseDate").Value), _
                                  TotalComment = Integer.Parse(x.Element("TotalComment").Value), _
                                  IsMedia = Boolean.Parse(x.Element("IsMedia").Value), _
                                  IsPicture = Boolean.Parse(x.Element("IsPicture").Value), _
                                  ArticlePassword = x.Element("ArticlePassword").Value, _
                                  PostVisible = x.Element("PostVisible").Value, _
                                  ArticlePath = x.Element("ArticlePath").Value, _
                                  Approved = Boolean.Parse(x.Element("Approved").Value), _
                                  ImagePath = x.Element("ImagePath").Value, _
                                  IsVote = Boolean.Parse(x.Element("IsVote").Value), _
                                  CategoryId = Integer.Parse(x.Element("CategoryId").Value), _
                                  ArrayCatID = x.Element("ArrayCatID").Value).SingleOrDefault()


                ArticlesId = articleSource.ArticlesID
                Title = articleSource.Title
                Body = articleSource.Body
                CommentEnabled = articleSource.CommentEnabled
                ViewCount = articleSource.ViewCount
                ReleaseDate = articleSource.ReleaseDate
                TotalComment = articleSource.TotalComment
                IsMedia = articleSource.IsMedia
                IsPicture = articleSource.IsPicture
                ArticlePassword = articleSource.ArticlePassword
                PostVisible = articleSource.PostVisible
                ArticlePath = articleSource.ArticlePath
                Approved = articleSource.Approved
                ImagePath = articleSource.ImagePath
                IsVote = articleSource.IsVote
                CategoryId = articleSource.CategoryId
                ArrayCatID = articleSource.ArrayCatID
            End If
        End Sub

        Public Sub WriteXmlbyId(ByVal id As Integer, ByVal day As Integer, ByVal month As Integer, ByVal year As Integer, ByVal Body As String, ByVal CategoryID As Integer, ByVal ArrayCatID As String)
            Dim strPath As String

            If Not Directory.Exists(Configs.ArticleFile & "ListFile\" & year.ToString() & "\" & month.ToString() & "\" & day.ToString() & "\") Then
                Directory.CreateDirectory(Configs.ArticleFile & "ListFile\" & year.ToString() & "\" & month.ToString() & "\" & day.ToString() & "\")
            End If

            strPath = Configs.ArticleFile & "ListFile\" & year.ToString() & "\" & month.ToString() & "\" & day.ToString() & "\" & id.ToString() & ".xml"

            Dim xmlWrite As New XmlTextWriter(strPath, Encoding.UTF8)

            xmlWrite.WriteStartDocument()
            xmlWrite.WriteStartElement("Articles")
            xmlWrite.WriteStartElement("Article", vbNullString)
            xmlWrite.WriteElementString("ArticlesID", ArticlesId.ToString)
            xmlWrite.WriteElementString("Title", Title)
            xmlWrite.WriteElementString("Body", HttpContext.Current.Server.HtmlEncode(Body))
            xmlWrite.WriteElementString("CommentEnabled", CommentEnabled.ToString)
            xmlWrite.WriteElementString("ViewCount", ViewCount.ToString)
            xmlWrite.WriteElementString("ReleaseDate", ReleaseDate.ToString)
            xmlWrite.WriteElementString("TotalComment", TotalComment.ToString)
            xmlWrite.WriteElementString("IsMedia", IsMedia.ToString)
            xmlWrite.WriteElementString("IsPicture", IsPicture.ToString)
            xmlWrite.WriteElementString("IsVote", IsVote.ToString)
            xmlWrite.WriteElementString("ArticlePassword", ArticlePassword)
            xmlWrite.WriteElementString("PostVisible", PostVisible)
            xmlWrite.WriteElementString("ArticlePath", ArticlePath)
            xmlWrite.WriteElementString("Approved", Approved.ToString)
            xmlWrite.WriteElementString("ImagePath", ImagePath)
            xmlWrite.WriteElementString("CategoryId", CategoryID.ToString)
            xmlWrite.WriteElementString("ArrayCatID", ArrayCatID)
            xmlWrite.WriteEndElement()
            xmlWrite.WriteEndElement()
            xmlWrite.Flush()
            xmlWrite.Close()
        End Sub

        Public Sub WriteRss(ByVal catId As Integer)
            Try
                Dim strOut As New StringBuilder()
                Dim strPath As String
                CategoryId = catID
                If catID = 1000000 Then
                    ArticlePrv.sqlDbRead = ArticlePrv.GetTop20NoCat()
                    strPath = Configs.RSSFile & "Trang-Chu.xml"
                    CatName = "Trang Chủ"
                    While ArticlePrv.sqlDbRead.Read()
                        strOut.Append("<item>")
                        strOut.Append("<title><![CDATA[").Append(HttpContext.Current.Server.HtmlDecode(ArticlePrv.sqlDbRead("Title").ToString())).Append(" ]]></title>")
                        strOut.Append("<description><![CDATA[").Append(HttpContext.Current.Server.HtmlDecode(ArticlePrv.sqlDbRead("Abstract").ToString())).Append(" ]]></description>")
                        strOut.Append("<link>").Append(Left(Configs.HomeUrl, Configs.HomeUrl.Length - 1) & ArticlePrv.sqlDbRead("ArticlePath").ToString).Append("</link>")
                        strOut.Append("<pubDate>").Append(Util.GetRFC822Date(DateTime.Parse(ArticlePrv.sqlDbRead("ReleaseDate").ToString()))).Append("</pubDate>")
                        strOut.Append("<guid>").Append(Left(Configs.HomeUrl, Configs.HomeUrl.Length - 1) & ArticlePrv.sqlDbRead("ArticlePath").ToString).Append("</guid>")
                        strOut.Append("</item>")
                    End While
                    ArticlePrv.sqlDbRead.Close()
                Else
                    ArticlePrv.sqlDbRead = ArticlePrv.GetByCatInside(Me)

                    If ArticlePrv.sqlDbRead.Read Then
                        CatName = ArticlePrv.sqlDbRead("CategoryName")
                        strPath = Configs.RSSFile & Util.utf8_to_ascii(CatName) & ".xml"
                    Else
                        ArticlePrv.sqlDbRead.Close()
                        Exit Sub
                    End If

                    ArticlePrv.sqlDbRead.NextResult()

                    While ArticlePrv.sqlDbRead.Read()
                        strOut.Append("<item>")
                        strOut.Append("<title><![CDATA[").Append(HttpContext.Current.Server.HtmlDecode(ArticlePrv.sqlDbRead("Title").ToString())).Append(" ]]></title>")
                        strOut.Append("<description><![CDATA[").Append(HttpContext.Current.Server.HtmlDecode(ArticlePrv.sqlDbRead("Abstract").ToString())).Append(" ]]></description>")
                        strOut.Append("<link>").Append(Left(Configs.HomeUrl, Configs.HomeUrl.Length - 1) & ArticlePrv.sqlDbRead("ArticlePath").ToString).Append("</link>")
                        strOut.Append("<pubDate>").Append(Util.GetRFC822Date(DateTime.Parse(ArticlePrv.sqlDbRead("ReleaseDate").ToString()).AddHours(Configs.TimeLine))).Append("</pubDate>")
                        strOut.Append("<guid>").Append(Left(Configs.HomeUrl, Configs.HomeUrl.Length - 1) & ArticlePrv.sqlDbRead("ArticlePath").ToString).Append("</guid>")
                        strOut.Append("</item>")
                    End While
                    ArticlePrv.sqlDbRead.Close()
                End If

                Dim strWrite As New StreamWriter(strPath)

                strWrite.Write("<?xml version=""1.0"" encoding=""utf-8""?>")
                strWrite.Write("<rss version=""2.0"">")
                strWrite.Write("<channel>")
                strWrite.Write("<title>" & Configs.WebName & " - " & CatName & "</title>")
                strWrite.Write("<description>" & Configs.WebName & " - " & Configs.WebDescription & "</description>")
                strWrite.Write("<generator>MeoTom's Blog Editor 2.0</generator>")
                strWrite.Write("<managingEditor>Admin</managingEditor>")
                strWrite.Write("<webMaster>webmaster@meotom.net</webMaster>")
                strWrite.Write("<link>" & Configs.HomeUrl & "</link>")
                strWrite.Write("<copyright>MeoTom's Blog</copyright>")
                strWrite.Write("<pubDate>" + Util.GetRFC822Date(DateTime.Now.AddHours(Configs.TimeLine)) + "</pubDate>")
                strWrite.Write("<lastBuildDate>" + Util.GetRFC822Date(DateTime.Now.AddHours(Configs.TimeLine)) + "</lastBuildDate>")
                strWrite.Write("<image><title>" & Configs.WebName & " - " & CatName & "</title><url>" & Configs.HomeUrl & "images/logo.gif</url><link>" & Configs.HomeUrl & "</link><width>384</width><height>95</height></image>")
                strWrite.Write("<ttl>5</ttl>")
                strWrite.Write(strOut.ToString())
                strWrite.Write("</channel>")
                strWrite.Write("</rss>")
                strWrite.Flush()
                strWrite.Close()
            Catch ex As Exception

            End Try
        End Sub

        'Public Function WriteArticleByCat(ByVal item As Integer) As String
        '    Dim iCount As Integer = 1
        '    Dim iCountItem As Integer = 0
        '    Dim configItem As Integer
        '    Dim catTitle As String = ""
        '    Dim imageArticle As String
        '    Dim strOut As New StringBuilder()
        '    Dim strContent As New StringBuilder()
        '    Dim strTopCt As New StringBuilder()
        '    Dim strTop3Ct As New StringBuilder()
        '    Try
        '        Using dc As New MeoTomDataContext
        '            Dim configSource = (From cf In dc.tbl_Configs Where cf.Name = "Display Dev" Select cf.Value).SingleOrDefault

        '            strOut.Append("<table cellpadding=""0"" cellspacing=""0"" border=""0"" width=""100%"">")
        '            strOut.Append("     <tr>")
        '            strOut.Append("         <td>")
        '            For Each articleSource In From ConfigItem As Integer In configSource.Split(",") Select (From a In dc.Articles Join b In dc.TempArticleCats On a.ArticlesID Equals b.ArticleID _
        '                Join c In dc.ArticleCategories On b.CatID Equals c.CatID Join d In dc.Justin_tblUsers On d.ID Equals a.UserID _
        '                Where d.SiteID = Convert.ToInt32(Configs.SiteID) And a.ReleaseDate < DateTime.Now And a.Approved = True _
        '                      And b.CatID = ConfigItem Order By a.ReleaseDate Descending _
        '                Select New Articles With {.ArticlesId = a.ArticlesID, .Title = a.Title, .TitleNone = a.TitleNone, _
        '                    .CreatedDate = a.CreatedDate, .ReleaseDate = a.ReleaseDate, .Approved = a.Approved, _
        '                    .ArticlePath = a.ArticlePath, .PostVisible = a.PostVisible, .ViewCount = a.ViewCount, _
        '                    .IsMedia = a.IsMedia, .IsPicture = a.IsPicture, .ImagePath = a.ImagePath, .CatName = c.Title, .Abstract = a.Abstract}).Take(item)
        '                For Each ArticleItem In articleSource
        '                    catTitle = ArticleItem.CatName

        '                    If ArticleItem.ImagePath = "" Or ArticleItem.ImagePath = vbNullString Then
        '                        imageArticle = "/template/PSP/images/no_image_.jpg"
        '                    Else
        '                        imageArticle = Configs.UserVirtualFolder & ArticleItem.ImagePath
        '                    End If

        '                    If iCountItem = 0 Then
        '                        strTopCt.Append("<div class=""ArticleItem"">")
        '                        strTopCt.Append("   <div class=""ArticleTitle""><a href=""").Append(ArticleItem.ArticlePath).Append(""" title=""").Append(ArticleItem.Title).Append("""><h4>").Append(ArticleItem.Title).Append("</h4></a></div>")
        '                        strTopCt.Append("   <div class=""ArticleImage""><a href=""").Append(ArticleItem.ArticlePath).Append(""" title=""").Append(ArticleItem.Title).Append("""><img alt=""").Append(ArticleItem.Title).Append(""" src=""").Append(imageArticle).Append(""" /></a></div>")
        '                        strTopCt.Append("   <div class=""ArticleDesc"">").Append(Left(HttpContext.Current.Server.HtmlDecode(ArticleItem.Abstract), 150)).Append("</div>")
        '                        strTopCt.Append("   <div class=""ArticleMore""><a href=""").Append(ArticleItem.ArticlePath).Append(""" title=""").Append(ArticleItem.Title).Append(""">Read more</a></div>")
        '                        strTopCt.Append("</div>")
        '                    Else
        '                        strTop3Ct.Append("   <li><a href=""").Append(ArticleItem.ArticlePath).Append(""" title=""").Append(ArticleItem.Title).Append(""">").Append(ArticleItem.Title).Append("</a></li>")
        '                    End If

        '                    iCountItem = iCountItem + 1
        '                Next
        '                iCountItem = 0

        '                strOut.Append("<div class=""ArticleBox fl"">")
        '                strOut.Append("     <h3>").Append(catTitle).Append("</h3>")
        '                strOut.Append("     <div class=""hr fl"">&nbsp;</div>")
        '                strOut.Append("     <div class=""content-box"">")
        '                strOut.Append(strTopCt)
        '                strOut.Append("         <ul>")
        '                strOut.Append(strTop3Ct)
        '                strOut.Append("         </ul>")
        '                strOut.Append("     </div>")
        '                strOut.Append("</div>")

        '                If iCount Mod 3 = 0 Then
        '                    strOut.Append("         </td>")
        '                    strOut.Append("     </tr>")
        '                    strOut.Append("     <tr>")
        '                    strOut.Append("         <td>")
        '                End If

        '                catTitle = ""
        '                strContent.Remove(0, strContent.Length)
        '                strTopCt.Remove(0, strTopCt.Length)
        '                strTop3Ct.Remove(0, strTop3Ct.Length)
        '                iCount = iCount + 1
        '            Next

        '            Dim objTag As New Tags
        '            Dim tPrv As New TagProvider
        '            Dim strCloudTemp As String
        '            strOut.Append("<div class=""ArticleBox autoheight fl""><h3>Tags</h3><div class=""hr fl"">&nbsp;</div><div class=""content-box""><ul id=""tagCloud"">")
        '            strCloudTemp = "<li><a rel=""nofollow"" href=""/t$urlencodetagid$/$urlencodetag$"" class=""weight$weight$"" title=""$tag$"">$tag$</a></li>"
        '            strOut.Append(objTag.GetCloud(tPrv.GetTagData().Tables(0), strCloudTemp))
        '            strOut.Append("</ul>")
        '            strOut.Append("</div></div>")

        '            strOut.Append("         </td>")
        '            strOut.Append("     </tr>")
        '            strOut.Append("</table>")
        '        End Using
        '    Catch ex As Exception
        '        'strOut.Append(ex.Message)
        '    End Try
        '    Return strOut.ToString
        'End Function
    End Class
End Namespace